//
//  UseTimeViewController.m
//  tuanGG
//
//  Created by 李洪旭 on 15/11/19.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "UseTimeViewController.h"
#import "LqkinDataPicker.h"
#import "UserTimeTableViewCell.h"
@interface UseTimeViewController ()<UITableViewDataSource,UITableViewDelegate,LqkinDataPickerDelegate>
@property(nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)NSMutableArray * exitTimeArray;

@property (nonatomic,strong)NSString * startTime;
@property (nonatomic,strong)NSString * endTime;
@property (nonatomic,strong)NSString * shTime;
@property (nonatomic,strong)NSString * ehTime;
@property (nonatomic,strong)NSString * smTime;
@property (nonatomic,strong)NSString * emTime;
@property (nonatomic,assign)NSInteger selectIndex;
@property (nonatomic,strong)NSIndexPath * oldindexpath;
@property (nonatomic,strong)UITableView *tableView ;
@end

@implementation UseTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0efed"];
    [self addnavigationTitle:@"使用时间" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self addTableView];
}
- (void)initlizeDataSource {

    _dataArray = @[@"开始时间",@"结束时间"];
    _exitTimeArray = [[NSMutableArray alloc]init];
    
    if ([User_member instance].usetime != nil && ![[User_member instance].usetime isEqualToString:@""]) {
        [_exitTimeArray addObjectsFromArray:[[User_member instance].usetime componentsSeparatedByString:@"-"]];
    }
    
}
- (void)addTableView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 90)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.tableFooterView = [[UITableView alloc]init];
    [_tableView registerClass:[UserTimeTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
//    cell.textLabel.font = [UIFont systemFontOfSize:14];
//    cell.textLabel.numberOfLines = 0;
    cell.titleName.text = _dataArray[indexPath.row];
    if (_exitTimeArray.count != 0 && _exitTimeArray != nil) {
        cell.timelabel.text = [NSString stringWithFormat:@"%@",_exitTimeArray[indexPath.row]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _selectIndex = indexPath.row;
    _oldindexpath = indexPath;
    if (indexPath.row == 0) {
        
        //开始时间
        LqkinDataPicker * picker = [[LqkinDataPicker alloc]initWithFrame:CGRectMake(0, -64, ScreenWidth, ScreenHeight)type:0];
        picker.delegate = self;
        [self.view addSubview:picker];
    }
    else if (indexPath.row == 1)
    {
        //结束时间
        if (_startTime == nil || [_startTime isEqualToString:@""]) {
            
            [Common addAlert:@"请优先选择开始时间"];
            return;
        }
        else
        {
            LqkinDataPicker * picker = [[LqkinDataPicker alloc]initWithFrame:CGRectMake(0, -64, ScreenWidth, ScreenHeight)type:0];
            picker.delegate = self;
            [self.view addSubview:picker];
        }
        
      
    }
    
}

- (void)LqkinPikerViewDelegateDateyear:(NSString *)year dmonth:(NSString *)month dday:(NSString *)day hour:(NSString *)hour minute:(NSString *)minute timestamp:(NSString *)timestamp
{
    if (_selectIndex == 0) {
        //开始时间
        _startTime = [NSString stringWithFormat:@"%@:%@",hour,minute];
        _shTime = [NSString stringWithFormat:@"%@",hour];
        _smTime = [NSString stringWithFormat:@"%@",minute];
        UserTimeTableViewCell * cell = (UserTimeTableViewCell*)[_tableView cellForRowAtIndexPath:_oldindexpath];
        cell.timelabel.text =_startTime;
    }
    else
    {
        //结束时间
        
        
        _ehTime = [NSString stringWithFormat:@"%@",hour];
        _emTime = [NSString stringWithFormat:@"%@",minute];
        _endTime = [NSString stringWithFormat:@"%@:%@",hour,minute];
        UserTimeTableViewCell * cell = (UserTimeTableViewCell*)[_tableView cellForRowAtIndexPath:_oldindexpath];
        cell.timelabel.text =_endTime;
        if ([_shTime integerValue] == [_ehTime integerValue]) {
            if ([_smTime integerValue] > [_emTime integerValue]) {
                [Common addAlert:@"结束时间不能比开始时间早"];
                return;
            }
            else
            {
                [self NetWorking];
            }
        }
        else if ([_shTime integerValue] > [_ehTime integerValue])
        {
            
                [Common addAlert:@"结束时间不能比开始时间早"];
                return;
        }
        else
        {
            
                //
              [self NetWorking];
            
        }
        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self NetWorking];
    }
}
- (void)NetWorking {
   
    NSString * newStr = [NSString string];
    newStr = [NSString stringWithFormat:@"%@-%@",_startTime,_endTime];
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObject:newStr forKey:@"content"];
    [GLAFRequest startRequest:usetime_url paramsDic:param defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue ] == 1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {
        
    }];
}

@end
